package userConsole.servlet;

import org.json.JSONException;
import org.json.JSONObject;
import userConsole.service.CheckUsernameService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * @author Godv
 * @version 1.0
 */
@WebServlet("/checkUsernameServlet")
public class CheckUsernameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        CheckUsernameService checkUsernameService = new CheckUsernameService();
        try {
            JSONObject jsonObject = checkUsernameService.checkUsername(username);
            response.setContentType("application/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print(jsonObject);
        } catch (SQLException | JSONException e) {
            e.printStackTrace();
        }
    }
}
